package com.gitee.feizns.explore.data_structure.graph.utils;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author feizns
 * @since 2020/2/27
 */
public class UndirectedGraph<K> {

    private Map<K, Set<K>> tables = new HashMap<>();

    public void addEdge(K s, K t) {
        addEdgeStT(s, t);
        addEdgeStT(t, s);
    }

    private void addEdgeStT(K s, K t) {
        Set<K> ks = tables.get(s);
        if ( ks == null )
            ks = new HashSet<>();
        ks.add(t);
        tables.put(s, ks);
    }

    public Set<K> nodes() {
        return tables.keySet();
    }

    public Set<K> adj(K k) {
        Set<K> ks = tables.get(k);
        return ks != null ? ks : Collections.emptySet();
    }

}
